/******************************************************************************
 **
 **    LISA Confidential
 **
 **    Source Materials
 **
 **    (C) COPYRIGHT LISA 2008-2009
 **
 **
 **  File Name:      GetCurrentTaskInstancesCommand.java
 **  Description:
 **  Date:           2008-10-27
 **  Author:         LisaQian
 ******************************************************************************/
package com.ti.andon.workflow.command;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmContext;
import org.jbpm.command.Command;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;

public class GetCurrentTaskInstancesCmd implements Command
{
	/**
	 * Serial Version ID. 
	 */
	private static final long serialVersionUID = -6777440108764765818L;

	private static Log logger = LogFactory.getLog(GetCurrentTaskInstancesCmd.class);
	
	// The business key of the current instance.
	String businessKey;
	// The process definition name of the process instance.
	String processDefinitionName;

	public GetCurrentTaskInstancesCmd(String businessKey, String processDefinition)
	{
		this.businessKey = businessKey;
		this.processDefinitionName = processDefinition;
	}

	/**
	 * Retrieves the collection of the signalling tasks of the process instance
	 * according to the process definition name and business key.
	 * 
	 * @param jbpmContext
	 * @return the collection of the signalling tasks of the process instance
	 *         according to the process definition name and business key.
	 */
	public Object execute(JbpmContext jbpmContext) throws Exception
	{		
		Collection allTaskInstances = null;		
		try
		{
			if (null != this.processDefinitionName && !this.processDefinitionName.equals("")
					&& null != this.businessKey)
			{
				List pds = jbpmContext.getGraphSession().findAllProcessDefinitionVersions(this.processDefinitionName);
				if (null != pds)
				{
					Iterator it = pds.iterator();
					ProcessInstance processInstance = null;					
					while (it.hasNext())
					{
						ProcessDefinition pd = (ProcessDefinition) it.next();
						processInstance = jbpmContext.getGraphSession().getProcessInstance(pd, businessKey);
						if (null != processInstance)
						{
							Token rootToken = processInstance.getRootToken();
							ExecutionContext ec = new ExecutionContext(rootToken);
							allTaskInstances = processInstance.getTaskMgmtInstance().getSignallingTasks(ec);
							break;
						}
					}
					if (null == processInstance)
					{
						logger
								.error("GetCurrentTaskInstancesCmd.execute() : Can not find the process instance according to the process definition name '"
										+ processDefinitionName + "' and business key '" + businessKey + "'!");
						throw (new Exception(
								"GetCurrentTaskInstancesCmd.execute() : Can not find the process instance according to the process definition name '"
										+ processDefinitionName + "' and business key '" + businessKey + "'!"));
					}
					else if (null == allTaskInstances)
					{
						logger
								.error("GetCurrentTaskInstancesCmd.execute() : Can not find signalling tasks for the process instance according to the process definition name '"
										+ processDefinitionName + "' and business key'" + businessKey + "'!");
						throw (new Exception(
								"GetCurrentTaskInstancesCmd.execute() : Can not find signalling tasks for the process instance according to the process definition name '"
										+ processDefinitionName + "' and business key'" + businessKey + "'!"));
					}
				}
				else
				{
					logger
							.error("GetCurrentTaskInstancesCmd.execute() : Can not find any process definition according to the process definition name'"
									+ processDefinitionName + "'!");
					throw (new Exception(
							"GetCurrentTaskInstancesCmd.execute() : Can not find any process definition according to the process definition name'"
									+ processDefinitionName + "'!"));
				}
			}
			else
			{
				logger
						.error("GetCurrentTaskInstancesCmd.execute() : Either process definition name or business key is null or a empty string!");
				throw (new Exception("GetCurrentTaskInstancesCmd.execute() : Either process definition name or business key is null or a empty string!!"));
			}
		}
		catch (Exception e)
		{
			logger.error("GetCurrentTaskInstancesCmd.execute() : Exception occurred : " + e.toString());
			throw (new Exception(e));
		}
		
	    return allTaskInstances;
	}

}
